package com.fwpsl.springframework;

import com.fwpsl.springframework.entity.User;
import com.fwpsl.springframework.service.IJdkProxyService;
import com.fwpsl.springframework.service.Impl.AopDemoServiceImpl;
import com.fwpsl.springframework.service.Impl.CglibProxyDemoServiceImpl;
import com.fwpsl.springframework.service.Impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @author: 风戏fw
 * @date: 2024/5/20
 * @description:
 */
public class App {

    public static void main(String[] args) {

//        iocTest();

//        aopTest();
        aopTest2();
    }

    private static void iocTest() {
        // xml配置方法
//        ApplicationContext context =
//                new ClassPathXmlApplicationContext("aspects.xml", "daos.xml", "services.xml");

        // Java配置
//        ApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);

        // 注解配置
        ApplicationContext context = new AnnotationConfigApplicationContext(
                "com.fwpsl.springframework");

        // retrieve configured instance
//        UserServiceImpl service = context.getBean("userService", UserServiceImpl.class);
        UserServiceImpl service = context.getBean(UserServiceImpl.class);

        // use configured instance
        List<User> userList = service.findUserList();

        // print info from beans
        userList.forEach(a -> System.out.println(a.getName() + "," + a.getAge()));
    }

    private static void aopTest() {

        // create and configure beans
        ApplicationContext context = new ClassPathXmlApplicationContext("aspects1.xml");

        // retrieve configured instance
        AopDemoServiceImpl service = context.getBean("demoService", AopDemoServiceImpl.class);

        // use configured instance
        service.doMethod1();
        service.doMethod2();
        try {
            service.doMethod3();
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }

    private static void aopTest2() {

        // create and configure beans
        ApplicationContext context = new AnnotationConfigApplicationContext(
                "com.fwpsl.springframework");

        // retrieve configured instance
//        IJdkProxyService service = context.getBean(IJdkProxyService.class);
        CglibProxyDemoServiceImpl service = context.getBean(CglibProxyDemoServiceImpl.class);

        // use configured instance
        service.doMethod1();
        service.doMethod2();
        try {
            service.doMethod3();
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }
}